﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Text;
using RecordTime.Data;
using RecordTime.Repository.Interfaces;
using RecordTime.Repository.Repositories;

namespace RecordTime.Repository
{
    [Export(typeof(IUnitOfWork))]
    public class UnitOfWork : IUnitOfWork
    {
        private readonly RecordTime.Data.RecordingModelContainer _context = new RecordingModelContainer();
        private ITaskRepository _taskRepository;

        public ITaskRepository TaskRepository
        {
            get
            {
                if (this._taskRepository == null)
                    _taskRepository = new TaskRepository(_context);
                return _taskRepository;
            }
        }

        #region Implementation of IUnitOfWork

        public void Save()
        {
            _context.SaveChanges();
        }

        #endregion

        #region Implementation of IDisposable

        private bool _disposed = false;

        protected virtual void Dispose(bool disposing)
        {
            if (!this._disposed)
            {
                if (disposing)
                {
                    _context.Dispose();
                }
            }
            this._disposed = true;
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }


        #endregion
    }
}
